package com.changwu.cwbase.AppTest.DesignPattern;

/**
 * Created by changwu on 2018/10/13.
 */

public class FactoryDesign {


    /**
     * ==============简单工厂类======================
     */
    public interface Car {
        void run();
    }

    public static class Audi implements Car {

        @Override
        public void run() {
            System.out.println("奥迪在跑！");
        }

    }

    public static class Byd implements Car {

        @Override
        public void run() {
            System.out.println("比亚迪在跑！");
        }

    }

    public static class SimpleFactory {

            public static <T extends Car> T createCar(Class<T> clz) {
                Car car = null;
                try {
                    car = (Car) Class.forName(clz.getName()).newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return (T) car;
            }
    }

    /**
     * ==============工厂方法模式======================
     */
    public interface CarFactoryMethod {
        <T extends Car> T createCar(Class<T> clz);
    }

    public static class FactoryMethod implements CarFactoryMethod {
        @Override
        public <T extends Car> T createCar(Class<T> clz) {
            Car car = null;
            try {
                car = (Car) Class.forName(clz.getName()).newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return (T) car;
        }

    }


    /**
     * ==============抽象工厂方法模式======================
     */

    /**
     * 汽车抽象工厂类
     */
    interface CarAbstractFactory {
        <T extends Engine> T createEngine(Class<T> clz);
    }

    /**
     * 引擎接口
     *
     * @author Administrator
     */
    public interface Engine {
        void run();

        void start();
    }

    /**
     * 高档引擎
     *
     * @author Administrator
     */
    public static class LuxuryEngine implements Engine {

        @Override
        public void run() {
            System.out.println("转的快！");
        }

        @Override
        public void start() {
            System.out.println("启动快!可以自动启停！");
        }

    }

    /**
     * 低档引擎
     *
     * @author Administrator
     */
    public static class LowEngine implements Engine {

        @Override
        public void run() {
            System.out.println("转的慢！");
        }

        @Override
        public void start() {
            System.out.println("启动慢!");
        }

    }

    public static class AbstractFactory implements CarAbstractFactory{

            @Override
            public <T extends Engine> T createEngine(Class<T> clz) {
                Engine engine = null;
                try {
                    engine = (Engine) Class.forName(clz.getName()).newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return (T) engine;
            }

    }


    /*简单工厂模式(静态工厂模式)*/
    public void testSimpleFactory() {
        Car c1 = SimpleFactory.createCar(Audi.class);
        Car c2 = SimpleFactory.createCar(Byd.class);
        c1.run();
        c2.run();
    }
    /*工厂方法模式*/
    public  void testFactoryMethod() {
        Car c1 = new FactoryMethod().createCar(Audi.class);
        Car c2 = new FactoryMethod().createCar(Byd.class);
        c1.run();
        c2.run();

    }
    /*抽象工厂模式*/
    public  void testAbstractFactory() {
        AbstractFactory  factoryA = new AbstractFactory();
        AbstractFactory  factoryB = new AbstractFactory();
        Engine eA = factoryA.createEngine(LuxuryEngine.class);
        Engine eB = factoryB.createEngine(LowEngine.class);
        eA.run();
        eA.start();
        eB.run();
        eB.start();
    }
}
